package com.admin.utils.generate;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

public class CodeUtil {
    public static void main(String[] args) {
        //代码生成器对象
        AutoGenerator generator = new AutoGenerator ();
        //1配置全局配置
        GlobalConfig gc = new GlobalConfig ();
        String projectPath = System.getProperty ("user.dir") + "/src/main/java";
        gc.setOutputDir (projectPath);
        gc.setAuthor ("缘不易");
        gc.setOpen (false);
        gc.setFileOverride (true);
        gc.setServiceName ("%sService");
        gc.setIdType (IdType.AUTO);
        gc.setDateType (DateType.ONLY_DATE);
        gc.setSwagger2 (true);
        gc.setBaseResultMap (true);
        gc.setBaseColumnList (true);

        //设置数据源
        DataSourceConfig dsc = new DataSourceConfig ();
        dsc.setDbType (DbType.MYSQL);
        dsc.setUrl ("jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false");
        dsc.setDriverName ("com.mysql.cj.jdbc.Driver");
        dsc.setUsername ("root");
        dsc.setPassword ("root");
        //策略配置
        StrategyConfig strategy = new StrategyConfig ();
        //要生成的表
        strategy.setInclude ("feedback");
        strategy.setNaming (NamingStrategy.underline_to_camel);
        strategy.setColumnNaming (NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel (true);
        strategy.setControllerMappingHyphenStyle (true);
        strategy.setRestControllerStyle (true);
        strategy.setChainModel (true);
        TableFill creatTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time",FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> list = new ArrayList<>();
        list.add(creatTime);
        list.add(updateTime);
        strategy.setTableFillList(list);
        //包配置
        PackageConfig pc = new PackageConfig ();
        pc.setParent ("com.admin");
        pc.setController ("controller");
        pc.setService ("service");
        pc.setMapper ("mapper");
        pc.setXml ("mapper.xml");
        pc.setEntity ("entity");
        generator.setGlobalConfig (gc);
        generator.setDataSource (dsc);
        generator.setPackageInfo (pc);
        generator.setStrategy (strategy);
        //执行代码配置代码
        generator.execute ();

    }
}
